<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta name="generator" content="HTML Tidy for Windows (vers 11 August 2008), see www.w3.org" />

  <title>Canvas tutorial</title>
  <script type="text/javascript" src="./imagedecode.js">
</script>
  <script type="text/javascript" src="./neuralnetwork.js">
</script>
  <script type="text/javascript">
//<![CDATA[
    function computeThresholdValue(imageData) {
        var k;
        var i;
        var csum;
        var thresholdValue = 1;
        var ihist=[];
        for (i = 0; i < 0x100; i++) {
            ihist[i] = 0;
        }
        var gmin = 0xff;
        var gmax = 0;
    
        var length = imageData.data.length / 4;
        for (var i = 0; i < length; i++) {
            var cn = imageData.data[i * 4 + 0];
            ihist[cn]++;
            if (cn > gmax) {
                gmax = cn;
            }
            if (cn < gmin) {
                gmin = cn;
            }
        }
    
        var sum = csum = 0.0;
        var n = 0;
        for (k = 0; k <= 0xff; k++) {
            sum += k * ihist[k];
            n += ihist[k];
        }
        if (n == 0) {
            return 60;
        }
        var fmax = -1.0;
        var n1 = 0;
        for (k = 0; k < 0xff; k++) {
            n1 += ihist[k];
            if (n1 != 0) {
                var n2 = n - n1;
                if (n2 == 0) {
                    return thresholdValue;
                }
                csum += k * ihist[k];
                var m1 = csum / n1;
                var m2 = (sum - csum) / n2;
                var sb = ((n1 * n2) * (m1 - m2)) * (m1 - m2);
                if (sb > fmax) {
                    fmax = sb;
                    thresholdValue = k;
                }
            }
        }
        return thresholdValue;
    }
  //]]>
  </script>
  <script type="text/javascript">
//<![CDATA[
    function binarize(imageData) {
    var tv = 139;
    var length = imageData.data.length / 4;
    for (var i = 0; i < length; i++) {
        var r = imageData[i * 4 + 0];
        if (r >= tv) {
            imageData.data[i * 4 + 0] = 0xff;
            imageData.data[i * 4 + 1] = 0xff;
            imageData.data[i * 4 + 2] = 0xff;
        } else {
            imageData.data[i * 4 + 0] = 0;
            imageData.data[i * 4 + 1] = 0;
            imageData.data[i * 4 + 2] = 0;
        }
    }
    return imageData;
    }    
  //]]>
  </script>
  <script type="text/javascript">
//<![CDATA[
    function imagesplit(imageData) {
        
    }
  //]]>
  </script>
</head>

<body>
  <div>
    <p class="classname"><button type="button" onclick="javascript:grayscale(document.getElementById('myImage'), true);">image</button> <button type="button" onclick="javascript:decodeImage('myImage');">split</button></p>

    <p><img id="myImage" alt="" onload="javascript:grayscale(this, true);" name="myImage" src="images/b1.jpg" /></p>
  </div>
</body>
</html>
